//
//  SheetLayout.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/28.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class SheetLayout: UICollectionViewFlowLayout {
 fileprivate var attributesArr: [UICollectionViewLayoutAttributes] = []
    var contenSizeHeight:CGFloat = 0.00
    var style:sheetStyle?
    init(style:sheetStyle) {
        super.init()
        self.style = style
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func prepare() {
        super.prepare()
        self.attributesArr.removeAll()
        itemSize = CGSize(width: CGFloat((kSCREEN_WIDTH-50)/3), height: 30)

        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        scrollDirection = .vertical
        self.scrollDirection = UICollectionViewScrollDirection.vertical
        
        let sectionCount:Int = (self.collectionView?.numberOfSections)!;
        var topY = 152.00
        var changeY = 0.00
        for i in 0..<sectionCount  {
        let itemsCount = collectionView?.numberOfItems(inSection: i) ?? 0
        for itemIndex in 0..<itemsCount {
            let indexPath = IndexPath(item: itemIndex, section: i)
            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            
            let itemDistanceX = 15+CGFloat(CGFloat(Int(itemIndex%3)) * (itemSize.width + 10))
            let nameDistanceX = 15+CGFloat(CGFloat(itemIndex)*(itemSize.width + 10))
            
            if style == sheetStyle.leverSelectStyle {
                if i == 0 {
                    attributes.frame = CGRect(x: itemDistanceX, y: 139+CGFloat(Int(itemIndex/3)*35), width: itemSize.width, height: itemSize.height)
                    topY = Double(attributes.frame.origin.y + itemSize.height)
                }else if i == 1 {
                    attributes.frame = CGRect(x: nameDistanceX, y: CGFloat(10+topY), width: kSCREEN_WIDTH, height: 12)
                    //                topY = Double(attributes.frame.origin.y)
                }else if i == 2 {
                    attributes.frame = CGRect(x: itemDistanceX , y: CGFloat(20+12+topY) + CGFloat((itemIndex/3*35)), width: itemSize.width, height: itemSize.height)
                    changeY = Double(attributes.frame.origin.y) + Double(itemSize.height)+20.00
                }else if i == 3 {
                    attributes.frame = CGRect(x: nameDistanceX, y: CGFloat(changeY), width: kSCREEN_WIDTH, height: 12)
                  changeY = Double(attributes.frame.origin.y) + Double(12)+20.00
                }else if i == 4 {
                    attributes.frame = CGRect(x: itemDistanceX, y: CGFloat(changeY) + CGFloat((itemIndex/3*35)), width: itemSize.width, height: itemSize.height)
                }else {
                    attributes.frame = CGRect(x: 15+CGFloat(itemIndex)*((kSCREEN_WIDTH-30)/2+5), y: CGFloat(topY)+240.00, width: (kSCREEN_WIDTH-40)/2, height: 40)
                    contenSizeHeight = attributes.frame.origin.y+40
                }
                
            }else {
                if i == 0 {
                    attributes.frame = CGRect(x: itemDistanceX, y: 139+CGFloat(Int(itemIndex/3)*35), width: itemSize.width, height: itemSize.height)
                    topY = Double(attributes.frame.origin.y + itemSize.height)
                }else if i == 1 {
                    attributes.frame = CGRect(x: nameDistanceX, y: CGFloat(10+topY), width: kSCREEN_WIDTH, height: 12)
                    //                topY = Double(attributes.frame.origin.y)
                }else if i == 2 {
                    attributes.frame = CGRect(x: nameDistanceX, y: CGFloat(20+12+topY), width: itemSize.width, height: itemSize.height)
                    
                    //                topY = Double(attributes.frame.origin.y)
                }else {
                    attributes.frame = CGRect(x: 15+CGFloat(itemIndex)*((kSCREEN_WIDTH-30)/2+5), y: CGFloat(topY)+86.00, width: (kSCREEN_WIDTH-40)/2, height: 40)
                    contenSizeHeight = attributes.frame.origin.y+40
                }
            }
            
            // 把每一个新的属性保存起来
            attributesArr.append(attributes)
         }
        }
        let layoutHeader =  UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, with: NSIndexPath(index: 0) as IndexPath)
        layoutHeader.frame = CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 139)
        attributesArr.append(layoutHeader)
    }
    
    override var collectionViewContentSize: CGSize {
        get {
            
            return CGSize(width: kSCREEN_WIDTH, height: contenSizeHeight+10)
        }
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        return attributesArr
    }
    
}

